package cn.wolfcode.luowowo.mgrsite.controller;

import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.domain.Region;
import cn.wolfcode.luowowo.article.service.IDestinationService;
import cn.wolfcode.luowowo.article.service.IRegionService;
import cn.wolfcode.luowowo.common.query.RegionQuery;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/region")
public class RegionController {
    @Reference
    private IDestinationService destinationService;
    @Reference
    private IRegionService regionService;

    @RequestMapping("/list")
    public String list(@ModelAttribute("qo") RegionQuery qo, Model model) {
        model.addAttribute("pageInfo",regionService.query(qo));
        return "region/list";
    }

    @ResponseBody
    @RequestMapping("/saveOrUpdate")
    public Object saveOrUpdate(Region region) {
        AjaxResult result = new AjaxResult();
        try {
            regionService.saveOrUpdate(region);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark("操作失败");
        }
        return result;

    }

    @ResponseBody
    @GetMapping("/listDestByRegionId")
    public Object listDestByRegionId(Long rid){
        List<Destination> destinations = destinationService.listDestByRegionId(rid);
        return destinations;
    }

    @ResponseBody
    @GetMapping("/listDests")
    public Object listDests() {
        List<Destination> destinations = destinationService.listDests();
        return destinations;
    }


}
